<?php

namespace app\auto;

use app\Log;
use app\model\Account;
use app\model\Store;

class CreateStoreAccount
{
    public function createAccount()
    {
        $store = app(Store::class);
        $account = app(Account::class);
        $log = app(Log::class, ['dir' => 'create_store_account']);
        //获取到未创建账号的
        $data = $store->whereNotIn('id', $account->whereIn('mobile', $store->column('mobile'))->column('store_id'))->where('is_del', '=', 1)->select()->toArray();
        $log->write(['params' => $data]);
        if (empty($data)) {
//            echo '没有可生成的账号\n';
            return "";
        }
        $its = [];
        foreach ($data as $value) {
            //设置密码
            $password = setPassword("123456");
            $_account = $value['mobile'];
            $time = bcadd(bcadd(array_sum(str_split(bcmul(microtime(true), 1000))), array_sum(str_split($value['mobile']))), rand(100, 999));
            $its[] = [
                'password' => $password['password'],
                'decode' => $password['encode'],
                'store_id' => $value['id'],
                'supplier_id' => $value['pid'],
                'account' => $_account . $time,
                'type' => 2,
                'balance' => 0,
                'name' => $value['username'],
                'mobile' => $value['mobile'],
                'is_del' => 1,
                'state' => 1
            ];
//            $account->save();
        }
        $account->exists(false)->saveAll($its);
        $log->write(['params' => $its]);
//        echo '生成成功\n';
    }
}